package com.jz.community.moduleshoppingguide.neighbor.ui.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gongwen.marqueen.MarqueeFactory;
import com.gongwen.marqueen.SimpleMarqueeView;
import com.jz.community.basecomm.bean.RegionsBean;
import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.RxDataTool;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.R2;
import com.jz.community.moduleshoppingguide.home.bean.PreferentialBean;
import com.jz.community.moduleshoppingguide.home.model.ShoppingGuideModel;
import com.jz.community.moduleshoppingguide.home.model.ShoppingModelGuideImpl;
import com.jz.community.moduleshoppingguide.home.ui.activity.PreferentialActivity;
import com.jz.community.moduleshoppingguide.nearshop.bean.ShopDownStairsBean;
import com.jz.community.moduleshoppingguide.nearshop.model.NearShopModel;
import com.jz.community.moduleshoppingguide.nearshop.model.NearShopModelImp;
import com.jz.community.moduleshoppingguide.nearshop.ui.activity.NearShopActivity;
import com.jz.community.moduleshoppingguide.neighbor.bean.CircleMessageBean;
import com.jz.community.moduleshoppingguide.neighbor.bean.CircleNoticeBean;
import com.jz.community.moduleshoppingguide.neighbor.task.CircleNoticeTask;
import com.jz.community.moduleshoppingguide.neighbor.ui.activity.NeighborMessageActivity;
import com.jz.community.moduleshoppingguide.neighbor.ui.adapter.NoticeViewMF;
import com.othershe.combinebitmap.CombineBitmap;
import com.othershe.combinebitmap.layout.WechatLayoutManager;
import com.sackcentury.shinebuttonlib.ShineButton;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NeighborHead {


    @BindView(R2.id.rl_circle_rel_img)
    ImageView rlCircleRelImg;
    @BindView(R2.id.rl_circle_head_message_one_style)
    RelativeLayout rlCircleHeadMessageOneStyle;
    @BindView(R2.id.circleCommunityName)
    TextView circleCommunityName;
    @BindView(R2.id.tv_circle_rel_userCount)
    TextView tvCircleRelUserCount;
    @BindView(R2.id.iv_add_circle_btn)
    ShineButton ivAddCircleBtn;
    @BindView(R2.id.textView3)
    TextView textView3;
    @BindView(R2.id.rl_circle_head_message_one)
    CardView rlCircleHeadMessageOne;
    @BindView(R2.id.rl_circle_head_message)
    RelativeLayout rlCircleHeadMessage;
    @BindView(R2.id.tv_notice_rel)
    TextView tvNoticeRel;
    @BindView(R2.id.tx_marquee_rel_one)
    SimpleMarqueeView txMarqueeRel;
    @BindView(R2.id.rl_notice_info_rel)
    RelativeLayout rlNoticeInfoRel;
    @BindView(R2.id.iv_near_shop_in)
    ImageView ivNearShopIn;
    @BindView(R2.id.ll_into_near_shop_rel)
    LinearLayout llIntoNearShopRel;
    @BindView(R2.id.ll_discount_goods_in)
    LinearLayout llDiscountGoodsIn;
    @BindView(R2.id.iv_discount_goods_in)
    ImageView ivDiscountGoodsIn;
    @BindView(R2.id.ll_nearShop_discount_into_rel)
    LinearLayout llNearShopDiscountIntoRel;
    @BindView(R2.id.tv_ont_login_remind)
    TextView tvOntLoginRemind;
    @BindView(R2.id.rl_circle_head_message_style)
    RelativeLayout rlCircleHeadMessageStyle;
    private View headView;
    Context context;
    private NearShopModel nearShopModel;
    private ShoppingGuideModel shoppingModel;

    public NeighborHead(Context context, RecyclerView recyclerView) {
        headView = LayoutInflater.from(context).inflate(R.layout.neighbor_head, (ViewGroup) recyclerView.getParent(), false);
        ButterKnife.bind(this, headView);
        this.context = context;
    }


    public void setData(CircleMessageBean circleBean) {
        nearShopModel = new NearShopModelImp(context);
        shoppingModel = new ShoppingModelGuideImpl(context);
        if (circleBean.getHeadImages() != null && circleBean.getHeadImages().size() > 0) {
            loadBitmap(circleBean.getHeadImages().toArray(new String[circleBean.getHeadImages().size()]), context);
        }
        textView3.setText("已加入");
        tvOntLoginRemind.setVisibility(View.GONE);
        ivAddCircleBtn.setChecked(true);
        if (!RxDataTool.isNullString(circleBean.getCircleName())) {
            circleCommunityName.setText(circleBean.getCircleName());
        }
        if (Integer.parseInt(circleBean.getUserNum()) > 0) {
            tvCircleRelUserCount.setText(circleBean.getUserNum() + "人");
        }
        RegionsBean.EmbeddedBean.ContentBean region = BaseSpUtils.getInstance().getRegion(context);
        new CircleNoticeTask((Activity) context, o -> {
            if (o != null) {
                CircleNoticeBean circleNoticeBean = (CircleNoticeBean) o;
                if (circleNoticeBean.get_embedded().getContent() != null && circleNoticeBean.get_embedded().getContent().size() > 0) {
                    MarqueeFactory<TextView, CircleNoticeBean.EmbeddedBean.ContentBean> marqueeFactory = new NoticeViewMF(context);
                    marqueeFactory.setData(circleNoticeBean.get_embedded().getContent());
                    txMarqueeRel.setInAndOutAnim(R.anim.in_top, R.anim.out_bottom);
                    txMarqueeRel.setMarqueeFactory(marqueeFactory);
                    txMarqueeRel.startFlipping();
                }
            }
        }).execute(region.getId() + "");

        nearShopModel.showShopDownStairs(0, 5, circleBean.getLon(), circleBean.getLat(), "", "", "", 1, new OnLoadListener<ShopDownStairsBean>() {
            @Override
            public void onSuccess(ShopDownStairsBean shopDownStairsBean) {
                if (shopDownStairsBean != null && shopDownStairsBean.get_embedded().getShopDownStairsViews() != null & shopDownStairsBean.get_embedded().getShopDownStairsViews().size() > 0) {
                    GlideApp.with(context)
                            .load(shopDownStairsBean.get_embedded().getShopDownStairsViews().get(0).getLogo())
                            .placeholder(R.mipmap.near_shop_avatar_default)
                            .error(R.mipmap.near_shop_avatar_default)
                            .into(ivNearShopIn);
                }
            }

            @Override
            public void onFail(String msg, int code) {

            }
        });

        shoppingModel.preferentialArea(0, 10, region.getId() + "", "0", new OnLoadListener<PreferentialBean>() {
            @Override
            public void onSuccess(PreferentialBean preferentialBean) {
                if (preferentialBean != null && preferentialBean.get_embedded().getContent() != null & preferentialBean.get_embedded().getContent().size() > 0) {
                    GlideApp.with(context)
                            .load(preferentialBean.get_embedded().getContent().get(0).getIcon())
                            .placeholder(R.mipmap.near_shop_avatar_default)
                            .error(R.mipmap.near_shop_avatar_default)
                            .into(ivDiscountGoodsIn);
                }
            }

            @Override
            public void onFail(String msg, int code) {

            }
        });
        addListener(circleBean, context);
    }

    private void addListener(CircleMessageBean circleBean, Context context) {
        rlCircleHeadMessage.setOnClickListener(v -> {
            Intent intent = new Intent(context, NeighborMessageActivity.class);
            intent.putExtra("circle_id", circleBean.getId());
            context.startActivity(intent);
        });
        llIntoNearShopRel.setOnClickListener(v -> {
            if (!TextUtils.isEmpty(circleBean.getLat()) && !TextUtils.isEmpty(circleBean.getLon())) {
                Intent intent = new Intent(context, NearShopActivity.class);
                intent.putExtra("fromFlag", 1);
                intent.putExtra("mLatitude", Double.parseDouble(circleBean.getLat()));
                intent.putExtra("mLongitude", Double.parseDouble(circleBean.getLon()));
                context.startActivity(intent);
            }
        });
        llDiscountGoodsIn.setOnClickListener(v -> context.startActivity(new Intent(context, PreferentialActivity.class)));
    }


    public View getHeadView() {
        return headView;
    }

    private void loadBitmap(String[] urls, Context context) {
        if (urls.length >= 4) {
            CombineBitmap.init(context)
                    .setLayoutManager(new WechatLayoutManager())
                    .setSize(180)
                    .setGap(3)
                    .setGapColor(Color.parseColor("#E8E8E8"))
                    .setUrls(urls)
                    .setImageView(rlCircleRelImg)
                    .setOnSubItemClickListener(index -> {
                    }).build();
        } else {
            GlideApp.with(context)
                    .load(R.mipmap.circle_default_img)
                    .placeholder(R.mipmap.circle_default_img)
                    .error(R.mipmap.circle_default_img)
                    .into(rlCircleRelImg);
        }
    }

}
